package main

import (
	"fmt"
	"reflect"
	"strconv"
	"time"
)

type base struct {
	num int
}

func (b base) test() string {
	return strconv.Itoa(b.num)
}

type coni struct {
	base
	str string
}

func functest23() {
	co := coni{
		base: base{
			num: 10,
		},
		str: "some name ",
	}
	fmt.Println("co.test():", co.test())
	type desc interface {
		test() string
	}
	var d desc = co
	fmt.Println("d.test():", d.test())
	fmt.Printf("%T  ,  %d\n", d, reflect.TypeOf(d))
}
func mainsdd() {
	messages := make(chan string, 2)

	messages <- "buffered"
	messages <- "channel"

	fmt.Println(<-messages)
	fmt.Println(<-messages)
	done := make(chan bool, 2)
	go worker(done)
	fmt.Println(<-done)

}
func worker(done chan bool) {
	fmt.Println("worker started")
	//time.Sleep(2 * time.Second)
	fmt.Println("worker done1")
	done <- true
	fmt.Println("worker done2")
	done <- true
	fmt.Println("worker done3")
}
func functest24() {
	c1 := make(chan string, 1)
	c2 := make(chan string, 1)
	go func() {
		time.Sleep(2 * time.Second)
		c1 <- "one"
	}()
	go func() {
		time.Sleep(2 * time.Second)
		c2 <- "two"
	}()
	for i := 0; i < 2; i++ {
		select {
		case msg1 := <-c1:
			fmt.Println(msg1)
		case msg2 := <-c2:
			fmt.Println(msg2)
		}
	}
}
func functest25() {
	fmt.Println("=============")
	c1 := make(chan string, 1)
	go func() {
		c1 <- "one"
		time.Sleep(2 * time.Second)
		c1 <- "one"
	}()
	select {
	case msg1 := <-c1:
		fmt.Println("one", msg1)
	case <-time.After(1 * time.Second):
		fmt.Println("time sceond 1 time out 2")
	}
	c2 := make(chan string, 1)
	go func() {
		time.Sleep(2 * time.Second)
		c2 <- "two"
	}()
	select {
	case msg2 := <-c2:
		fmt.Println(msg2)
	case <-time.After(1 * time.Second):
		fmt.Println("time sceond 2 time out 333")
	}

}
func functest26() {
	msg := make(chan string, 1)
	sigin := make(chan string, 1)
	select {
	case msg1 := <-msg:
		fmt.Println(msg1)
	default:
		fmt.Println("no msg")
	}
	msg2 := "hi"
	select {
	case msg <- msg2:
		fmt.Println(msg2)
	default:
		fmt.Println("no msg msg")
	}
	close(msg)
	fmt.Println("sigins times kso :", <-sigin)
	close(sigin)
	select {
	case mm := <-msg:
		fmt.Println(mm)
	case sigins := <-sigin:
		fmt.Println(sigins)
	default:
		fmt.Println("no msg sigins")
	}
}
func functest27() {
	jobs := make(chan string, 2)
	done := make(chan bool)

	go func() {
		for {
			j, more := <-jobs
			if more {
				fmt.Println(j)
			} else {
				fmt.Println(j)
				done <- true
				return
			}
		}
	}()
	for i := 0; i < 2; i++ {
		jobs <- strconv.Itoa(i)
		fmt.Println("jobs :", <-jobs)
	}
	fmt.Println("kso close jobs ....")
	close(jobs)
	close(done)
	fmt.Println("sent job i jobs close .....")
	<-done
	for {
		_, ok := <-jobs
		fmt.Println(ok)
	}

}
